package com.example.journalegocentrique;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.IntentAction;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.view.Window;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

//Preference's Activity

public class Preferences extends Activity {

	//Preparing SeekBars
	
	private SeekBar hourSeek;
	private SeekBar fontTypeSeek;
	private SeekBar fontSizeSeek;
	private TextView hours;
	private TextView textFont;
	public static final String PREFS_NAME = "MyPrefsFile";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.preferences);
		
		//Revenue persistent data
		SharedPreferences preferences = getSharedPreferences(PREFS_NAME,0);
		
		String fontStyl=preferences.getString("styleFont", "default");
		int fontSize=preferences.getInt("sizeFont", 15);
		int periodoNote=preferences.getInt("durationNote", 1);
		int fStyle=0;
		
		//ActionBar
		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar5);
		actionBar.setHomeAction(new IntentAction(this, Home.createIntent(this), R.drawable.ic_title_home_default));
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle("Settings");
		
		//SeekBar to change hours
		hourSeek=(SeekBar)findViewById(R.id.seekHours);
		hourSeek.setMax(23);
		hourSeek.setProgress(periodoNote-1);
		
		//SeekBar to change font
		fontTypeSeek=(SeekBar)findViewById(R.id.seekFontType);
		fontTypeSeek.setMax(10);
		if(fontStyl.compareTo("default")==0)
			fStyle=0;
		if(fontStyl.compareTo("advanced_pixel-7.ttf")==0)
			fStyle=1;
		if(fontStyl.compareTo("Byron Medium.ttf")==0)
			fStyle=2;
		if(fontStyl.compareTo("ColesFont.ttf")==0)
			fStyle=3;
		if(fontStyl.compareTo("ice_pixel-7.ttf")==0)
			fStyle=4;
		if(fontStyl.compareTo("led_display-7.ttf")==0)
			fStyle=5;
		if(fontStyl.compareTo("Leo Arrow.ttf")==0)
			fStyle=6;
		if(fontStyl.compareTo("mini_pixel-7.ttf")==0)
			fStyle=7;
		if(fontStyl.compareTo("neon_pixel-7.ttf")==0)
			fStyle=8;
		if(fontStyl.compareTo("old_pixel-7.ttf")==0)
			fStyle=9;
		if(fontStyl.compareTo("SamysBookifiedTuffy.ttf")==0)
			fStyle=10;
		
		fontTypeSeek.setProgress(fStyle);
		
		////SeekBar to change font size
		fontSizeSeek=(SeekBar)findViewById(R.id.seekFontSize);
		fontSizeSeek.setMax(10);
		fontSizeSeek.setProgress((fontSize-15)/5);
		
		hours=(TextView)findViewById(R.id.textHours);
		hours.setText("h: "+ (hourSeek.getProgress()+1));
		
		textFont=(TextView)findViewById(R.id.textFontType);
		
		Typeface myTypeface;
		if(fStyle!=0)
		{
			myTypeface = Typeface.createFromAsset(Preferences.this.getAssets(),fontStyl);
		}
		else
			myTypeface=Typeface.DEFAULT;
			
  		textFont.setTypeface(myTypeface);
  		textFont.refreshDrawableState();
		textFont.setText("La mia nota");
		textFont.setTextSize(fontSize);
		
		try {

		    //Listener to set the duration of the notes
		    hourSeek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
		    public void onStopTrackingTouch(SeekBar arg0) {
		    	SharedPreferences preferences = getSharedPreferences(PREFS_NAME,0);
		    	SharedPreferences.Editor editor = preferences.edit();
		    	int periodo = (arg0.getProgress()+1);
		    	editor.putInt("durationNote", periodo);
		    	editor.commit();
		        }
		 
		    public void onStartTrackingTouch(SeekBar arg0) {
		        }
		    //Progress changed SeekBar for hours
		    public void onProgressChanged(SeekBar arg0,
		        int progress, boolean arg2) {
		    	hours.setText("ore: "+ (progress+1));
		    }
		 });
		 
		    //Listener to set the font type
		    fontTypeSeek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
		   public void onStopTrackingTouch(SeekBar arg0) {
			    SharedPreferences preferences = getSharedPreferences(PREFS_NAME,0);
		    	SharedPreferences.Editor editor = preferences.edit();
		    	String font="default";
		    	switch(arg0.getProgress())
		    	{
		    		case 0:{font="default";}break;
		    		case 1:{font="advanced_pixel-7.ttf";}break;
		    		case 2:{font="Byron Medium.ttf";}break;
		    		case 3:{font="ColesFont.ttf";}break;
		    		case 4:{font="ice_pixel-7.ttf";}break;
		    		case 5:{font="led_display-7.ttf";}break;
		    		case 6:{font="Leo Arrow.ttf";}break;
		    		case 7:{font="mini_pixel-7.ttf";}break;
		    		case 8:{font="neon_pixel-7.ttf";}break;
		    		case 9:{font="old_pixel-7.ttf";}break;
		    		case 10:{font="SamysBookifiedTuffy.ttf";}break;
		    	}
		    	
		    	editor.putString("styleFont", font);
		    	editor.commit();
		        }
		 
		   public void onStartTrackingTouch(SeekBar arg0) {
		        }
		   //Progress changed SeekBar for font type
		   public void onProgressChanged(SeekBar arg0,
		        int progress, boolean arg2) {
			   switch(progress)
			   {
			   		case 0:{
			   		 Typeface myTypeface = Typeface.DEFAULT;
			   		 textFont.setTypeface(myTypeface);
			   		 textFont.refreshDrawableState();
			   		 textFont.setText("La mia nota");
			   		}break;
			   		case 1:{
			   			 Typeface myTypeface = Typeface.createFromAsset(Preferences.this.getAssets(),"advanced_pixel-7.ttf");
				   		 textFont.setTypeface(myTypeface);
				   		 textFont.refreshDrawableState();
				   		 textFont.setText("La mia nota");
			   		}break;
			   		case 2:{
			   			 Typeface myTypeface = Typeface.createFromAsset(Preferences.this.getAssets(),"Byron Medium.ttf");
				   		 textFont.setTypeface(myTypeface);
				   		 textFont.refreshDrawableState();
				   		 textFont.setText("La mia nota");
			   		}break;
			   		case 3:{
			   			 Typeface myTypeface = Typeface.createFromAsset(Preferences.this.getAssets(),"ColesFont.ttf");
				   		 textFont.setTypeface(myTypeface);
				   		 textFont.refreshDrawableState();
				   		 textFont.setText("La mia nota");
			   		}break;
			   		case 4:{
			   			 Typeface myTypeface = Typeface.createFromAsset(Preferences.this.getAssets(),"ice_pixel-7.ttf");
				   		 textFont.setTypeface(myTypeface);
				   		 textFont.refreshDrawableState();
				   		 textFont.setText("La mia nota");
			   		}break;
			   		case 5:{
			   			 Typeface myTypeface = Typeface.createFromAsset(Preferences.this.getAssets(),"led_display-7.ttf");
				   		 textFont.setTypeface(myTypeface);
				   		 textFont.refreshDrawableState();
				   		 textFont.setText("La mia nota");
			   		}break;
			   		case 6:{
			   			 Typeface myTypeface = Typeface.createFromAsset(Preferences.this.getAssets(),"Leo Arrow.ttf");
				   		 textFont.setTypeface(myTypeface);
				   		 textFont.refreshDrawableState();
				   		 textFont.setText("La mia nota");
			   		}break;
			   		case 7:{
			   			Typeface myTypeface = Typeface.createFromAsset(Preferences.this.getAssets(),"mini_pixel-7.ttf");
				   		 textFont.setTypeface(myTypeface);
				   		 textFont.refreshDrawableState();
				   		 textFont.setText("La mia nota");
			   		}break;
			   		case 8:{
			   			 Typeface myTypeface = Typeface.createFromAsset(Preferences.this.getAssets(),"neon_pixel-7.ttf");
				   		 textFont.setTypeface(myTypeface);
				   		 textFont.refreshDrawableState();
				   		 textFont.setText("La mia nota");
			   		}break;
			   		case 9:{
			   			 Typeface myTypeface = Typeface.createFromAsset(Preferences.this.getAssets(),"old_pixel-7.ttf");
				   		 textFont.setTypeface(myTypeface);
				   		 textFont.refreshDrawableState();
				   		 textFont.setText("La mia nota");
			   		}break;
			   		case 10:{
			   			 Typeface myTypeface = Typeface.createFromAsset(Preferences.this.getAssets(),"SamysBookifiedTuffy.ttf");
				   		 textFont.setTypeface(myTypeface);
				   		 textFont.refreshDrawableState();
				   		 textFont.setText("La mia nota");
			   		}break;
			   
			   }
			   
		     }
		 });
		 
		    //Listener to set the font size
		    fontSizeSeek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
		   public void onStopTrackingTouch(SeekBar arg0) {
			   	SharedPreferences preferences = getSharedPreferences(PREFS_NAME,0);
		    	SharedPreferences.Editor editor = preferences.edit();
		    	int size = 15+(arg0.getProgress()*5);
		    	editor.putInt("sizeFont", size);
		    	editor.commit();
		        }
		 
		   public void onStartTrackingTouch(SeekBar arg0) {
		        }
		 //Progress changed SeekBar for font size
		   public void onProgressChanged(SeekBar arg0,
		        int progress, boolean arg2) {
			   textFont.setTextSize(15 +(progress*5));
		    }
		 });
		 
		} catch (Exception e) {
		    e.printStackTrace();
		}			
	}
	
	@Override
	protected void onPause() 
	 {
		super.onPause();
		SharedPreferences preferences = getSharedPreferences(PREFS_NAME,0);
    	SharedPreferences.Editor editor = preferences.edit();
    	//Save font size in persistent data
    	int size = 15+(fontSizeSeek.getProgress()*5);
    	editor.putInt("sizeFont", size);
    	//Save font type in persistent data
    	String font="default";
    	switch(fontTypeSeek.getProgress())
    	{
    		case 0:{font="default";}break;
    		case 1:{font="advanced_pixel-7.ttf";}break;
    		case 2:{font="Byron Medium.ttf";}break;
    		case 3:{font="ColesFont.ttf";}break;
    		case 4:{font="ice_pixel-7.ttf";}break;
    		case 5:{font="led_display-7.ttf";}break;
    		case 6:{font="Leo Arrow.ttf";}break;
    		case 7:{font="mini_pixel-7.ttf";}break;
    		case 8:{font="neon_pixel-7.ttf";}break;
    		case 9:{font="old_pixel-7.ttf";}break;
    		case 10:{font="SamysBookifiedTuffy.ttf";}break;
    	}
    	editor.putString("styleFont", font);
    	//Save duration note in persistent data
    	int periodo = (hourSeek.getProgress()+1);
    	editor.putInt("durationNote", periodo);
    	    	
    	editor.commit();
	 }

}
